package hos.okhttp.http;


import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import hos.http.HttpSoapUtils;
import hos.http.HttpTag;
import hos.okhttp.exception.OkHttpException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okhttp3.internal.Util;

/**
 * <p>Title: OkHttpInterceptor </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/3/1 21:12
 */
public class OkHttpInterceptor implements Interceptor {

    private int retryCount = 0;

    public OkHttpInterceptor(int retryCount) {
        this.retryCount = retryCount;
    }

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();
        try {
            // 上传监听
            HttpTag.UploadListeners uploadListeners = request.tag(HttpTag.UploadListeners.class);
            if (uploadListeners != null && !uploadListeners.isEmpty()) {
                ProgressRequestBody progressRequestBody = new ProgressRequestBody(uploadListeners, request.body());
                request = request.newBuilder().method(request.method(), progressRequestBody).build();
            }
            Response response = chain.proceed(request);
            if (this.retryCount > 0) {
                // 重试
                int retryCount = 0;
                while (!response.isSuccessful() && retryCount < this.retryCount) {
                    retryCount++;
                    Util.closeQuietly(response);
                    response = chain.proceed(request);
                }
            }
            // 下载监听
            HttpTag.DownloadListeners downloadListeners = request.tag(HttpTag.DownloadListeners.class);
            if (downloadListeners != null && !downloadListeners.isEmpty()) {
                ProgressResponseBody progressResponseBody = new ProgressResponseBody(downloadListeners, response.body());
                return response.newBuilder().body(progressResponseBody).build();
            }
            return buildSoapResponse(request, response);
        } catch (SocketTimeoutException e) {
            throw OkHttpException.throwTimeout(request, e);
        } catch (ConnectException e) {
            throw OkHttpException.throwConnection(request, e);
        } catch (UnknownHostException e) {
            throw OkHttpException.throwUnknown(request, e);
        }
    }

    protected Response buildSoapResponse(Request request, Response response) {
        // soap 方法名
        HttpTag.Soap methodName = request.tag(HttpTag.Soap.class);
        if (methodName != null && methodName.name != null && methodName.name.length() != 0) {
            // 对Xml进行处理
            ResponseBody body = response.body();
            if (body != null) {
                String parseXml = HttpSoapUtils.parseSoapXml(body.byteStream(), methodName.name);
                if (parseXml != null) {
                    ResponseBody responseBody = ResponseBody.create(parseXml, body.contentType());
                    return response.newBuilder().body(responseBody).build();
                }
            }
        }
        return response;
    }
}
